'''
翻转一棵二叉树。

示例：

输入：

     4
   /   \
  2     7
 / \   / \
1   3 6   9
输出：

     4
   /   \
  7     2
 / \   / \
9   6 3   1

来源：力扣（LeetCode）
链接：https://leetcode-cn.com/problems/invert-binary-tree
著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
'''
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def invertTree(self, root: TreeNode) -> TreeNode:
        stack = []
        if not root:
            return root
        stack.append(root)
        while len(stack) > 0:
            tmp = stack.pop()
            if tmp.left != None:
                stack.append(tmp.left)
            if tmp.right != None:
                stack.append(tmp.right)
            tmp.left, tmp.right = tmp.right, tmp.left
        return root